import Toast from './../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('./../../utils/request').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'',
    array:[{name:'身份证',value:1}],
    zhengjian:'',
    phone:'',
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindzjChange:function(e) {
    this.setData({
      zhengjian: e.detail.value
    })
  },

  getPhoneNumber (e) {
    if( e.detail.errMsg == 'getPhoneNumber:ok' ){
      $api.getUserPhone({
        encryptedData:e.detail.encryptedData,
        iv:e.detail.iv,
        session_key:wx.getStorageSync('session_key'),
        openid:wx.getStorageSync('openid')
      }).then(res=>{
        console.log(res.data)
        this.setData({
          phone:res.data
        })
      }).catch(err=>{
        wx.showToast({
          title: '获取手机号失败',
          icon:'error'
        })
      })
 
    }else{
      wx.showToast({
        title: '获取手机号失败',
        icon:'none'
      })
    }
  },

  formSubmit(e) {
    if(this.data.phone == ''){
      return Toast.fail('请点击按钮获取手机号');
    }
  if(e.detail.value.date == ''){
      return Toast.fail('请选择出生日期');
    }
    let params = {
      "addr": e.detail.value.addr,
      "birthday": e.detail.value.date,
      "cname": e.detail.value.cname,
      "idNo":e.detail.value.number,
      "idType": e.detail.value.number? "1":"",
      "openId": wx.getStorageSync('openid'),
      "phone": this.data.phone
    }

    $api.becomeMember(params).then(res=>{
      if( res.code = 200 ){
        wx.setStorageSync('phone', this.data.phone)
        wx.showToast({
          title: '注册成功',
          icon:'success'
        })
        setTimeout(() => {
          wx.switchTab({
            url:'../home/home'
          })
        }, 1000);
  
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    }).catch(err=>{

    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})